<?php # RoomReservation.php
session_start();
require "hotels.class.php";
require "customers.class.php";
require "roombookings.class.php";
require "bookings.class.php";
require "rooms.class.php";
require "roomtypes.class.php";
require "payments.class.php";
require "paymentmethods.class.php";

header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="utf-8"
standalone="yes" ?>
<response>';

$debug = false;

/*
 * Specify the POST field names that are in the form.
 */
$allowedPostFields = array(
			'hotelId',
			'roomTypeCode',
			'smokingYN',
 			'indate',
			'outdate',
			'numguests', // the number of guests staying in the room.
			'paymentType' // the payment type is Visa, MasterCard, Discover, AMX, etc. From the PaymentMethods table
);

/*
 * Specify the POST field names that are required...
 */
$requiredPostFields = array(
			'hotelId',
			'roomTypeCode',
			'smokingYN',
			'indate',
			'outdate'
);

/*
 * Specify the session field names that are in the form.
 */
$allowedSessionFields = array(
 			'customerId',
			'user_name'
);

/*
 * Specify the session field names that are required...
 */
$requiredSessionFields = array(
			'customerId'
);

$error = false;
foreach($_POST AS $key => $value)
{
	// first make sure this is an allowed field
	if(in_array($key, $allowedPostFields))
	{
		$$key = $value;
		
		// is this a required field?
		if(in_array($key, $requiredPostFields) && $value == '')
		{
			$error = true;
			echo '<error>The field '.$key.' is required.</error>';
		}
	}
	else
	{
		$error = true;
		echo '<error>The field '.$key.' does not exist.</error>';
	}
}

foreach($_SESSION AS $key => $value)
{
	// first make sure this is an allowed field
	if(in_array($key, $allowedSessionFields))
	{
		$$key = $value;
		
		// is this a required field?
		if(in_array($key, $requiredSessionFields) && $value == '')
		{
			$error = true;
			echo '<error>The field '.$key.' is required.</error>';
		}
	}
	else
	{
		$error = true;
		echo '<error>The field '.$key.' does not exist.</error>';
	}
}

if (!$error) {
	$customerId = '';
	if (isset($_SESSION['customerId'])){
		$customerId = $_SESSION['customerId'];
	}
	else {
		$customerId = '';
	}
	
	// check to make sure that the customer exists.
	// if the customer exists, then continue to make the reserveration.
	$customer = Customers::loadUnique($customerId);
  	if($customer->getCustomerId() == $customerId) {
		if (isset($_POST['indate'])){
			$searchindate = strtotime($_POST['indate']);
		}
		else {
			$searchindate = '';
		}
		if (isset($_POST['outdate'])){
			$searchoutdate = strtotime($_POST['outdate']);
		}
		else {
			$searchoutdate = '';
		}
		if (isset($_POST['roomTypeCode'])){
			$searchroomtypecode = $_POST['roomTypeCode'];
		}
		else {
			$searchroomtypecode = '';
		}
		if (isset($_POST['smokingYN'])){
			$searchsmoking = $_POST['smokingYN'];
		}
		else {
			$searchsmoking = '';
		}
		if (isset($_POST['hotelId'])){
			$searchhotelid = $_POST['hotelId'];
		}
		else {
			$searchhotelid = '';
		}
		if (isset($_POST['paymentType'])){
			$paymentType = $_POST['paymentType'];
		}
		else {
			$paymentType = '';
		}
		if (isset($_POST['numguests'])){
			$numguests = $_POST['numguests'];
		}
		else {
			$numguests = 1;
		}
	
		// check to ensure that the hotel exists with the given hotelId.
		// if the hotel exists than continue to make the room reservation in the specified hotel.
		$hotel = Hotels::loadUnique($searchhotelid);
		if($hotel->getHotelId() == $searchhotelid) {
			$return_arr = array();
		
			// check to ensure that the specified roomType exists with the given roomTypeCode.
			// if the room type exists than register a room that has that room type.
			$roomType = Roomtypes::loadUnique($searchroomtypecode);
			if($roomType->getRoomTypeCode() == $searchroomtypecode) {
				// retrieve the rooms for the hotel with the specified roomTypeCode
				$hotelRooms = Rooms::loadByRoomTypeCodeAndHotelId($searchroomtypecode, $searchhotelid);
				$totalRooms = count($hotelRooms);
				$isRoomAvailable = false;
				for ($j = 0; $j < $totalRooms; $j++) {  // loop through all the rooms to find the first available room based upon the search criteria
					$roomId = $hotelRooms[$j]->getRoomId();
					$roomType = $hotelRooms[$j]->getRoomtypes();
					$roomTypeCode = $roomType->getRoomTypeCode();
					$roomStandardRate = $roomType->getRoomStandardRate();
					$roomTypeDescription = $roomType->getRoomTypeDescription();
					$roomSmokingYN = $roomType->getSmokingYN();
			
					// search for room types based on smoking
					if($roomSmokingYN != $searchsmoking) { // Room doesn't match the smoking search criteria.  Continue to next room.
						continue;
					}
			
					// get the room bookings if any
					$roomBookings = $hotelRooms[$j]->getRoombookingss(); // array
					$totalRoomBookings = count($roomBookings);
					if($totalRoomBookings == 0) { // room is available because it has never been booked, so assign this room.
						$isRoomAvailable = true;
					}
					else {
						// check to see if the room is available based upon the search criteria for indate and outdate.
						for ($k = 0; $k < $totalRoomBookings; $k++) {
							$booking = $roomBookings[$k]->getBookings();
							$dateFrom = strtotime($booking->getDateFrom());
							$dateTo = strtotime($booking->getDateTo());
					
							if(($searchindate < $dateFrom && $searchoutdate <= $dateFrom) || ($searchindate >= $dateTo && $searchoutdate > $dateTo)) {
								$isRoomAvailable = true;
							}
							else {
								$isRoomAvailable = false;
							}
						}
					}
					if($isRoomAvailable == true) { // register the room
						$todaysDate = date("Y-m-d");
						// create a new booking
						$booking = new Bookings;
						$booking->setCustomerId($customerId);
						$booking->setDateBookingMade($todaysDate);
						$booking->setDateFrom($_POST['indate']);
						$booking->setDateTo($_POST['outdate']);
						$booking->setPaymentDueDate($_POST['outdate']);
						$booking->setPaymentDueAmount($roomStandardRate);
						$booking->setPaymentMadeOn($_POST['outdate']);
						$booking->setComments('');
						$bookingId = $booking->insert();
						$booking->setBookingId($bookingId);
							
						// create a roombooking
						$roombooking = new Roombookings;
						$roombooking->setHotelId($searchhotelid);
						$roombooking->setBookingId($bookingId);
						$roombooking->setRoomId($roomId);
						$roombooking->setNumGuests($numguests);
						$roombooking->insert();
						
						// create a payment
						$paymentMethod = Paymentmethods::loadByPaymentMethod($paymentType); // get the payment method for the specified payment type
						if(count($paymentMethod) > 0) {
							$paymentMethodId = $paymentMethod[0]->getPaymentMethodId();
							$payment = new Payments;
							$payment->setCustomerId($customerId);
							$payment->setBookingId($bookingId);
							$payment->setPaymentMethodId($paymentMethodId);
							$payment->setPaymentAmount($roomStandardRate);
							$payment->setPaymentComments('');
							$paymentId = $payment->insert();
							$payment->setPaymentId($paymentId);
						}
						echo '<result>Success: RoomId - '.$roomId.' is available.</result>';
						break; // exit the loop because a room has been registered.
					}
					else {
						if($debug == true)
							echo '<error>Room Id: '.$roomId.' is not available.</error>';
					}
				}
				if($isRoomAvailable == false) {
					echo '<error>No rooms are available with the specified search criteria.</error>';
				}
			}
			else {
				echo '<error>The room type code '.$searchroomtypecode.' does not exist.</error>';
			}
		}
		else {
			echo '<error>The hotel with id '.$searchhotelid.' does not exist.</error>';
		}
	}
	else {
		echo '<error>The customer with id '.$customerId.' does not exist.</error>';
	}
}

echo '</response>';
?>
